package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppUserTypeBo;
import com.ruoyi.app.domain.vo.AppUserTypeVo;
import com.ruoyi.app.domain.AppUserType;
import com.ruoyi.app.mapper.AppUserTypeMapper;
import com.ruoyi.app.service.IAppUserTypeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * app用户类型Service业务层处理
 *
 * @author Zach
 * @date 2023-11-14
 */
@RequiredArgsConstructor
@Service
public class AppUserTypeServiceImpl implements IAppUserTypeService {

    private final AppUserTypeMapper baseMapper;

    /**
     * 查询app用户类型
     */
    @Override
    public AppUserTypeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询app用户类型列表
     */
    @Override
    public TableDataInfo<AppUserTypeVo> queryPageList(AppUserTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUserType> lqw = buildQueryWrapper(bo);
        Page<AppUserTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询app用户类型列表
     */
    @Override
    public List<AppUserTypeVo> queryList(AppUserTypeBo bo) {
        LambdaQueryWrapper<AppUserType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUserType> buildQueryWrapper(AppUserTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppUserType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTypeName()), AppUserType::getTypeName, bo.getTypeName());
        lqw.eq(bo.getIsPay() != null, AppUserType::getIsPay, bo.getIsPay());
        lqw.eq(bo.getPayNumber() != null, AppUserType::getPayNumber, bo.getPayNumber());
        lqw.eq(bo.getFhConditionNum() != null, AppUserType::getFhConditionNum, bo.getFhConditionNum());
        lqw.eq(bo.getFhConditionMoney() != null, AppUserType::getFhConditionMoney, bo.getFhConditionMoney());
        lqw.eq(bo.getRebate() != null, AppUserType::getRebate, bo.getRebate());
        lqw.eq(bo.getRebateFirst() != null, AppUserType::getRebateFirst, bo.getRebateFirst());
        lqw.eq(bo.getRebateSecondary() != null, AppUserType::getRebateSecondary, bo.getRebateSecondary());
        lqw.eq(bo.getStatus() != null, AppUserType::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增app用户类型
     */
    @Override
    public Boolean insertByBo(AppUserTypeBo bo) {
        AppUserType add = BeanUtil.toBean(bo, AppUserType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改app用户类型
     */
    @Override
    public Boolean updateByBo(AppUserTypeBo bo) {
        AppUserType update = BeanUtil.toBean(bo, AppUserType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppUserType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除app用户类型
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
